import 'package:flutter/cupertino.dart';
import '../../public.dart';
import './slidableBar.dart';

class GroupPage extends StatefulWidget {
  const GroupPage({super.key});

  @override
  State<GroupPage> createState() => _GroupPageState();
}

class _GroupPageState extends State<GroupPage> {
  @override
  Widget build(BuildContext context) {
    return ProviderWidget<UserModel>(
      builder: (ctx, model, child) {
        return CupertinoPageScaffold(
          navigationBar: const CupertinoNavigationBar(
            border: Border(bottom: BorderSide.none),
            backgroundColor: Colors.white,
            middle: Text('Group Management'),
          ),
          child: SafeArea(
            child: Material(
              child: ListView.builder(
                itemCount: model.groups.length,
                itemBuilder: (context, index) {
                  Category category =
                      AccountList.categories[model.groups[index].type]
                          as Category;
                  return SlidableBar(
                    onDelete: () {
                      model.deleteGroup(model.groups[index].type);
                    },
                    child: ListTile(
                      onTap: () {
                        RouterHelper.navigator.pushNamed(
                          Routers.groupList,
                          arguments: model.groups[index].type,
                        );
                      },
                      leading: Image.asset(category.src, width: 40, height: 40),
                      tileColor: Colors.white,
                      contentPadding: const EdgeInsets.symmetric(
                        vertical: 12,
                        horizontal: 15,
                      ),
                      title: Row(
                        children: [
                          Text(category.name),
                          Text('(${model.groups[index].list.length})'),
                        ],
                      ),
                      trailing: const Icon(Icons.chevron_right),
                    ),
                  );
                },
              ),
            ),
          ),
        );
      },
      model: Provider.of<UserModel>(context),
    );
  }
}
